package com.hswealth;
/**
 *    @author    daixf41752
 *    @Date 2021-09-09 14:35:37    
 *	  @Description
 */

import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.ImageIcon;

public class EnemyPlane extends Thread{
	
	GameFrame gameFrame;
	
	int x, y;
	int width = 80, height = 80;
	int speed = 2;
	
	Image img = new ImageIcon("img/enemyplane.png").getImage();

	public EnemyPlane() {
		super();
	}
	
	

	public EnemyPlane(int x, int y, GameFrame gameFrame) {
		super();
		this.gameFrame = gameFrame;
		this.x = x;
		this.y = y;
	}



	public EnemyPlane(int x, int y, int width, int height) {
		super();
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	public void run() {
		while(true) {
			if (hit()) {
				this.speed = 0;
				this.img = new ImageIcon("img/die.png").getImage();
				try {
					this.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				gameFrame.enemyPlanes.remove(this);
				break;
			}
			
			if (this.y >= 590) {
				break;
			}
			try {
				this.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 	检测碰撞
	 * @return
	 */
	public boolean hit() {
		Rectangle myrect = new Rectangle(this.x, this.y, this.width, this.height);
		
		Rectangle rect = null;
		
		for (int i = 0; i < gameFrame.bullets.size() ; i++) {
			Bullet bullet = gameFrame.bullets.get(i);
			rect = new Rectangle(bullet.x, bullet.y, bullet.width, bullet.height);
			//检测碰撞
			if (myrect.intersects(rect)) {
				return true;
			}
		}
		return false;
	}

}
